﻿/*-----------------------------------------------------------------------------!
! SupplierInfo class                                                           !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : April 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : SupplierInfo.cs                                                   !
! Function : Supplier info page                                                !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Md. Shaiful Islam                                           !
! Last changed by: Md. Shaiful Islam, May 05 2011                              !
===============================================================================*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model.Sadharon;
using Model.Poribar.SupplierPanjeri;
using Model.Panjeri;
using InventorySys.controller;

namespace InventorySys.view
{
    public partial class SupplierInfo : Form
    {
        #region private method
        /// <summary>
        /// private method
        /// </summary>
        Result result = new Result();
        List<Model.Upadan.Supplier> listSupplier = new List<Model.Upadan.Supplier>();
        private SupplierController supplierController;
        #endregion

        #region constructor
        /// <summary>
        /// constructor
        /// </summary>
        public SupplierInfo()
        {
            InitializeComponent();
            supplierController = new SupplierController();
        }
        #endregion

        #region supplier all loading
        /// <summary>
        /// supplier all loading
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SupplierInfo_Load(object sender, EventArgs e)
        {
            supplierListLoad();
        }

        /// <summary>
        /// supplier list load 
        /// </summary>
        private void supplierListLoad()
        {
            ISupplierKaz sp = Panjeri.getSupplierPanjeri();
            result = sp.getList();
            if (result.IsSuccess)
            {
                listSupplier = (List<Model.Upadan.Supplier>)result.ResultObj;
                BindSupplierList(listSupplier);
            }
        }

        /// <summary>
        /// BindSupplierList
        /// </summary>
        /// <param name="listSupplier"></param>
        private void BindSupplierList(List<Model.Upadan.Supplier> listSupplier)
        {
            Model.Upadan.Supplier supplier = new Model.Upadan.Supplier();
            DtGrdSpList.Rows.Clear();
            for (int i = 0; i < listSupplier.Count; i++)
            {
                supplier = listSupplier[i];
                DtGrdSpList.Rows.Add();
                DtGrdSpList.Rows[i].Cells[0].Value = supplier.Id;
                DtGrdSpList.Rows[i].Cells[1].Value = supplier.AccountReference;
                DtGrdSpList.Rows[i].Cells[2].Value = supplier.CompanyName;
                DtGrdSpList.Rows[i].Cells[3].Value = supplier.ContactPerson;
                DtGrdSpList.Rows[i].Cells[4].Value = "Edit";
                DtGrdSpList.Rows[i].Cells[5].Value = "Delete";
            }
        }
        #endregion

        #region datagrid cell content click
        /// <summary>
        /// datagrid cell content click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DtGrdSpList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int i, j;
            i = int.Parse(e.RowIndex.ToString());
            j = int.Parse(e.ColumnIndex.ToString());
            if (j == 4)
            {
                string id = DtGrdSpList.Rows[i].Cells[0].Value.ToString();
                Supplier supplierForm = new Supplier();
                supplierForm.decidePageMode("viewMode", id);
                supplierForm.Show(this);
            }
            else if (j == 5)
            {
                string id = DtGrdSpList.Rows[i].Cells[0].Value.ToString();
                result = supplierController.deleteSupplierDetails(id);
                if (result.IsSuccess)
                {
                    MessageBox.Show(result.Message);
                    supplierListLoad();
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
                
            }
        }
        #endregion



    }
}
